package com.cyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.DoctorDao;
import com.cyz.dao.MessageDao;
import com.cyz.dao.OldDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.Doctor;
import com.cyz.entity.Old;
import com.cyz.entity.Remake;
import com.cyz.entity.User;
import com.cyz.vo.MessageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/doctor/message")
public class MessageController {

    @Autowired
    MessageDao messageDao;

    @Autowired
    UserDao userDao;

    @Autowired
    DoctorDao doctorDao;

    @Autowired
    OldDao oldDao;

    @PostMapping("/insert/{realName}")
    public String add(@PathVariable String realName, int id, String message){
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account", realName));
        String name = user.getName();
        Doctor doctor = doctorDao.selectOne(new QueryWrapper<Doctor>().eq("name", name));

        Remake message1 = new Remake();
        message1.setDoctorId(doctor.getId());
        message1.setOldId(id);
        message1.setMessage(message);
        int i = messageDao.insert(message1);
        return i == 0 ? "留言失败" : "留言成功";
    }

    @PostMapping("look")
    public List<MessageDTO> look(){
        return messageDao.selectList(new QueryWrapper<>()).stream().map(item -> {
            MessageDTO messageDTO = new MessageDTO();
            int doctorId = item.getDoctorId();
            int oldId = item.getOldId();

            Doctor doctor = doctorDao.selectById(doctorId);
            Old old = oldDao.selectById(oldId);

            messageDTO.setDoctorName(doctor.getName());
            messageDTO.setOldName(old.getName());
            messageDTO.setMessage(item.getMessage());
            return messageDTO;
        }).collect(Collectors.toList());
    }
}
